package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.*;
import com.jyy.prefabricated.entity.OrderInfo;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.service.SmsRecordService;
import com.jyy.prefabricated.service.SysUserService;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.StudentsOfficialVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户
 *
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private SmsRecordService smsRecordService;

    /**
     * 新增用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/users")
    public AjaxResult insert(@Valid @RequestBody SysUserDto dto) {
        log.info("Rest request to insert user:{}", dto);
        int id = userService.createUser(dto);
        SysUser user = userService.selectById(id);
        return AjaxResult.success(user);

    }

    /**
     * 修改用户
     *
     * @param dto
     * @return
     */
    @PutMapping("/users")
    public AjaxResult update(@Valid @RequestBody SysUserDto dto) {
        log.info("Rest request to update user:{}", dto);
        userService.update(dto);
        SysUser user = userService.selectById(dto.getId());
        return AjaxResult.success(user);
    }

    /**
     * 重置密码
     *
     * @param dto
     * @return
     */
    @PutMapping("/users-pwd")
    public AjaxResult resetPwd(@Valid @RequestBody ResetPwdDto dto) {
        log.info("Rest request to reset user password:{}", dto);
        int row = userService.resetPwdById(dto.getId(), dto.getPassword());
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改密码
     *
     * @param dto
     * @return
     */
    @PutMapping("/users-pwd-change")
    public AjaxResult changePwd(@Valid @RequestBody ChangePwdDto dto) {
        SysUser user = userUtils.getCurrentUser();
        log.info("Rest request to change user user:{},dto:{}", user, dto);
        int row = userService.changePwd(user, dto);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 设置用户角色
     *
     * @param dto
     * @return
     */
    @PutMapping("/users-roles")
    public AjaxResult putUserRoles(@Valid @RequestBody UserRoleDto dto) {
        log.info("Rest request to put user roles:{}", dto);
        userService.putUserRoles(dto);
        return AjaxResult.success();
    }

    /**
     * 修改用户状态
     *
     * @param id
     * @return
     */
    @PutMapping("/users-status/{id}")
    public AjaxResult updateStatus(@PathVariable Integer id) {
        log.info("Rest request to update user status by id:{}", id);
        int row = userService.updateStatus(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询用户详情
     *
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find user by id:{}", id);
        SysUser result = userService.selectById(id);
        return AjaxResult.success(result);
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/users/{id}")
    public AjaxResult deleteById(@PathVariable Integer id) {
        log.info("Rest request to delete user by id:{}", id);
        int row = userService.delete(id);
        if (row > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询用户
     *
     * @param keyword  关键字
     * @param status   状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/users")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false) Integer status, Pageable pageable) {
        log.info("Rest request to find user by params keyword:{},status:{}", keyword, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<SysUser> users = userService.selectByParams(keyword, status);
        PageInfo<SysUser> result = new PageInfo<>(users);
        return AjaxResult.success(result);
    }


    /**
     * 查询官网注册账号列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/students-official")
    public AjaxResult selectByDto(StudentsOfficialDto dto) {

        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<SysUser> users = userService.selectByDto(dto);
        PageInfo<SysUser> result = new PageInfo<>(users);
        return AjaxResult.success(result);
    }

    /**
     * 根据用户id查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/students-official/{id}")
    public AjaxResult selectByUserId(@PathVariable Integer id) {
        log.info("Rest request to find user by id:{}", id);
        StudentsOfficialVo result = userService.selectByUserId(id);
        return AjaxResult.success(result);
    }

    /**
     * 查询用户
     *
     * @param keyword 关键字
     * @param status  状态
     * @return
     */
    @GetMapping("/users-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String keyword, @RequestParam(required = false) Integer status) {
        log.info("Rest request to find user by params keyword:{},status:{}", keyword, status);
        List<SysUser> result = userService.selectByParams(keyword, status);
        return AjaxResult.success(result);
    }

    @GetMapping("/users-info")
    public AjaxResult fetchUserInfo() {
        SysUser result = userService.fetchUserInfo();
        return AjaxResult.success(result);
    }

    @PutMapping("/users-wxmp")
    public AjaxResult putOpenid(@RequestParam String openid) {
        SysUser user = userUtils.getCurrentUser();
        if (StringUtils.isBlank(user.getOpenid())) {
            user.setOpenid(openid);
            userService.update(user);
        }
        return AjaxResult.success(user);
    }

    @GetMapping("/salesperson")
    public AjaxResult selectSalesperson(SysUserDto sysUserDto){
        log.info("Rest request to find channelDto by params sysUserDto", sysUserDto);
        PageHelper.startPage(sysUserDto.getPageNum(), sysUserDto.getPageSize());
        List<SysUser> users = userService.selectSalesperson(sysUserDto);
        PageInfo<SysUser> result = new PageInfo<>(users);
        return AjaxResult.success(result);
    }
    @GetMapping("/allSalesperson")
    public AjaxResult allSalesperson(SysUserDto sysUserDto){
        log.info("Rest request to find channelDto by params sysUserDto", sysUserDto);
        List<SysUser> users = userService.allSelectSalesperson(sysUserDto);
        return AjaxResult.success(users);
    }
    @GetMapping("/salesperson/{id}")
    public AjaxResult selectSalesDetail(@PathVariable Integer id){
        SysUser result = userService.selectSalesDetail(id);
        return AjaxResult.success(result);
    }

    @GetMapping("/cusInviter")
    public AjaxResult selectByInviter(SysUserDto sysUserDto){
        log.info("Rest request to find channelDto by params sysUserDto", sysUserDto);
        PageHelper.startPage(sysUserDto.getPageNum(), sysUserDto.getPageSize());
        List<SysUser> users = userService.selectByInviter(sysUserDto);
        PageInfo<SysUser> result = new PageInfo<>(users);
        return AjaxResult.success(result);
    }

    @GetMapping("/orderList")
    public AjaxResult selectOrderList(SysUserDto sysUserDto){
        log.info("Rest request to find channelDto by params sysUserDto", sysUserDto);
        PageHelper.startPage(sysUserDto.getPageNum(), sysUserDto.getPageSize());
        List<OrderInfo> orderInfos = userService.selectOrderList(sysUserDto);
        PageInfo<OrderInfo> result = new PageInfo<>(orderInfos);
        return AjaxResult.success(result);
    }
}
